package com.atguigu.juc;

public class TestYield {

    private Integer num = 1;

    public static void main(String[] args) {

        TestYieldThread testYieldThread = new TestYieldThread();

        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                testYieldThread.get();
                if (testYieldThread.getNum() == 5) {
                    Thread.yield();
                }
            }
        },"AAAA").start();
        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                testYieldThread.get();
                if (testYieldThread.getNum() == 5) {
                    Thread.yield();
                }
            }
        },"BBBB").start();

    }


}

class TestYieldThread  {

    private  Integer num = 0;

    public  synchronized void get() {
        num++;
        System.out.println(Thread.currentThread().getName() + " : " + num);
    }


    public  Integer getNum() {
        return num;
    }
}
